<template>
  <div>
    <el-tabs v-model="activeName">
      <el-tab-pane label="出车记录" name="1" id="htmlxlsx">
        <div>
          <div class="pro_box">
            <div class="search_box">
              <el-input v-model="pag1.plate" placeholder="车牌"> </el-input>
              <el-select v-model="pag1.source" placeholder="车辆来源">
                <el-option value="1" label="自有"></el-option>
                <el-option value="2" label="租赁"></el-option>
              </el-select>
              <el-select v-model="pag1.is_bill1" placeholder="是否签单">
                <el-option value="1" label="已签单"></el-option>
                <el-option value="2" label="未签单"></el-option>
              </el-select>
              <el-date-picker
                v-model="pag1.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
              <el-button type="primary" @click="getList">搜索</el-button>
              <el-button @click="resetSearch">重置</el-button>
            </div>

            <div>
              <el-button type="primary" @click="addOneCar">出车录入</el-button>
              <el-button type="primary" @click="toBill">签单</el-button>
              <el-button type="primary" @click="printListData"
                >导出数据</el-button
              >
            </div>
          </div>
          <el-table
            :data="carsList"
            style="width: 100%"
            :height="h + 50"
            border
            :row-class-name="tableRowClassName"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55"
              :selectable="isSelectable"
            >
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="日期"
              align="center"
              width="160"
            >
              <template slot-scope="scope">
                <div>
                  {{
                    !Array.isArray(scope.row.create_time)
                      ? scope.row.start_time
                      : `${scope.row.start_time} - ${scope.row.end_time}`
                  }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="plate"
              label="车牌"
              align="center"
              width="120"
            >
              <template slot-scope="scope">
                <div
                  :style="{
                    color: scope.row.source == 1 ? '#67c23a' : '#f56c6c',
                  }"
                >
                  {{ scope.row.plate }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="specifications" label="规格" align="center">
            </el-table-column>
            <el-table-column
              prop="unit_price"
              label="单价"
              align="center"
              width="100px"
            >
            </el-table-column>
            <el-table-column prop="units" label="计量单位" align="center">
            </el-table-column>
            <el-table-column prop="agreed_time" label="额定工时" align="center">
            </el-table-column>
            <el-table-column prop="true_time" label="实际工时" align="center">
            </el-table-column>
            <el-table-column
              prop="price1"
              label="金额"
              align="center"
              width="100px"
            >
            </el-table-column>
            <el-table-column
              prop="overtime_unit"
              label="加班单价"
              align="center"
              width="100px"
            >
            </el-table-column>
            <el-table-column
              prop="overtime_hours"
              label="加班时长"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="overtime_price"
              label="加班费"
              align="center"
              width="100px"
            >
            </el-table-column>
            <el-table-column prop="other_price" label="其他费用" align="center">
            </el-table-column>
            <el-table-column
              prop="other_remark"
              label="其他费用说明"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="total_price"
              label="合计金额"
              align="center"
              width="110"
            >
            </el-table-column>
            <el-table-column prop="operator" label="操作人员" align="center">
            </el-table-column>
            <el-table-column prop="lessee" label="承租单位" align="center">
            </el-table-column>
            <el-table-column prop="dispatcher" label="派车人员" align="center">
            </el-table-column>
            <el-table-column prop="company" label="派车单位" align="center">
            </el-table-column>
            <el-table-column
              prop="content"
              label="工作内容"
              align="center"
              width="300"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="remark"
              label="备注"
              align="center"
              width="300"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="type"
              label="是否签单"
              align="center"
              fixed="right"
            >
              <template slot-scope="scope">
                <div
                  :style="{
                    color: scope.row.is_bill1 == 1 ? '#67c23a' : '#f56c6c',
                  }"
                >
                  {{ scope.row.is_bill1 == 1 ? "已签单" : "未签单" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="remark"
              label="操作"
              align="center"
              width="150"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-button type="text" @click="editCar(scope.row)"
                  >编辑</el-button
                >
                <el-button type="text" @click="delCar(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="page_table">
            <span style="line-height: 35px; margin-right: 30px"
              >总金额:
              <span style="color: red">{{ totalPrice.toFixed(4) }}</span></span
            >
            <el-pagination
              @size-change="handleSizeChange1"
              @current-change="handleCurrentChange1"
              :current-page="1"
              :page-sizes="[10, 20, 30, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pag1.total"
            >
            </el-pagination>
          </div>
        </div>
      </el-tab-pane>

      <!-- 出车录入 -->
      <el-tab-pane v-if="activeName == 2" label="出车录入" name="2">
        <div>
          <el-form
            :inline="true"
            :model="carForm"
            class="demo-form-inline"
            label-width="80px"
            ref="carsForm"
            :rules="carRules"
          >
            <el-form-item label="归属项目" prop="">
              <el-select
                v-model="carForm.project_id"
                @change="changeProject"
                filterable
              >
                <el-option
                  v-for="item in projectList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.project_name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出车类型" prop="type" style="width: 240px">
              <!-- <el-radio-group
                v-model="carForm.type"
                @change="changeGroup"
                ref="radios"
              >
                <el-radio label="1">台班</el-radio>
                <el-radio label="2">月租</el-radio>
              </el-radio-group> -->
              <el-select v-model="carForm.type" @change="changeGroup">
                <el-option value="1" label="台班"></el-option>
                <el-option value="2" label="月租"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="计量单位" prop="units">
              <el-input v-model="carForm.units"></el-input>
            </el-form-item>
            <el-form-item
              label="出车日期"
              prop="create_time"
              v-if="carForm.type == 1"
            >
              <el-date-picker
                v-model="carForm.create_time"
                type="date"
                placeholder="选择日期"
                value-format="yyyy/MM/dd"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item
              label="出车日期"
              prop="create_time"
              v-if="carForm.type == 2"
            >
              <el-date-picker
                v-model="carForm.create_time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy/MM/dd"
              >
              </el-date-picker
            ></el-form-item>

            <br />
            <el-form-item label="派车单位" prop="plate">
              <el-select
                v-model="carForm.company_id"
                @change="changeGs"
                filterable
              >
                <el-option
                  v-for="item in concatCompany"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item
              v-if="carForm.source == 1"
              label="车牌号"
              prop="plate"
            >
              <el-select v-model="carForm.plate" @change="changeplate" filterable>
                <el-option
                  v-for="item in myCars"
                  :key="item.id"
                  :value="item.id"
                  :label="item.plate"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-else label="车牌号" prop="plate">
              <el-input v-model="carForm.plate"></el-input>
            </el-form-item>
            <el-form-item label="车辆类型" prop="car_type">
              <el-input v-model="carForm.car_type"></el-input>
            </el-form-item>
            <el-form-item label="规格" prop="specifications">
              <el-input v-model="carForm.specifications">
                <template slot="append">吨</template>
              </el-input>
            </el-form-item>
            <el-form-item
              v-if="carForm.source == 2"
              label="用车成本"
              prop="cost"
            >
              <el-input v-model="carForm.cost" type="number"></el-input>
            </el-form-item>
            <br />

            <el-form-item label="单价" prop="unit_price">
              <el-input
                v-model="carForm.unit_price"
                @input="changeMoney(1)"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="额定工时" prop="agreed_time">
              <el-input
                v-model="carForm.agreed_time"
                @input="changeMoney(2)"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="实际工时" prop="true_time">
              <el-input
                v-model="carForm.true_time"
                @input="changeMoney(1)"
                type="number"
              ></el-input>
            </el-form-item>
            <br />

            <el-form-item label="加班单价" prop="overtime_unit">
              <el-input
                v-model="carForm.overtime_unit"
                @input="changeOverTime"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="加班工时" prop="overtime_hours">
              <el-input
                v-model="carForm.overtime_hours"
                @input="changeOverTime"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="加班费" prop="overtime_price">
              <el-input
                v-model="carForm.overtime_price"
                @input="changeOverTime"
                type="number"
              ></el-input>
            </el-form-item>
            <br />
            <el-form-item label="其他费用" prop="other_price">
              <el-input
                v-model="carForm.other_price"
                type="number"
                @input="changeMoney(1)"
              ></el-input>
            </el-form-item>
            <br />
            <el-form-item label="费用说明" prop="other_remark">
              <el-input
                v-model="carForm.other_remark"
                type="textarea"
                :rows="2"
              ></el-input>
            </el-form-item>
            <br />

            <el-form-item label="驾驶员" prop="driver1">
              <el-input v-model="carForm.driver1"></el-input>
            </el-form-item>
            <el-form-item label="提成比例" prop="driver1_commission">
              <el-input
                v-model="carForm.driver1_commission"
                type="number"
                @input="changeCommission"
              ></el-input>
            </el-form-item>
            <el-form-item label="提成金额" prop="driver1_price">
              <el-input v-model="carForm.driver1_price" disabled></el-input>
            </el-form-item>
            <br />

            <el-form-item label="随车人" prop="driver2">
              <el-input v-model="carForm.driver2"></el-input>
            </el-form-item>
            <el-form-item label="提成比例" prop="driver2_commission">
              <el-input
                v-model="carForm.driver2_commission"
                type="number"
                @input="changeCommission"
              ></el-input>
            </el-form-item>
            <el-form-item label="提成金额" prop="driver2_price">
              <el-input v-model="carForm.driver2_price" disabled></el-input>
            </el-form-item>
            <br />

            <el-form-item label="总金额" prop="total_price">
              <el-input v-model="carForm.total_price" disabled></el-input>
            </el-form-item>
            <el-form-item label="操作人" prop="operator">
              <el-input v-model="carForm.operator"></el-input>
            </el-form-item>
            <el-form-item label="派车人" prop="dispatcher">
              <el-input v-model="carForm.dispatcher"></el-input>
            </el-form-item>

            <br />
            <el-form-item label="工作内容" prop="content">
              <el-input
                v-model="carForm.content"
                type="textarea"
                :rows="2"
              ></el-input>
            </el-form-item>
            <br />

            <el-form-item label="其他应付" prop="other2_price">
              <el-input v-model="carForm.other2_price" type="number"></el-input>
            </el-form-item>
            <br />
            <el-form-item label="费用说明" prop="other2_remark">
              <el-input
                v-model="carForm.other2_remark"
                type="textarea"
                :rows="2"
              ></el-input>
              <span
                style="font-size: 12px; margin-left: 15px; margin-bottom: -8px"
              >
                应付"客户"的其他费用</span
              >
            </el-form-item>

            <br v-if="carForm.source == 2" />

            <el-form-item
              label="其他应收"
              prop="other1_price"
              v-if="carForm.source == 2"
            >
              <el-input v-model="carForm.other1_price" type="number"></el-input>
            </el-form-item>
            <br />
            <el-form-item
              label="费用说明"
              prop="other1_remark"
              v-if="carForm.source == 2"
            >
              <el-input
                v-model="carForm.other1_remark"
                type="textarea"
                :rows="2"
              ></el-input>
              <span style="font-size: 12px; margin-left: 15px"
                >应收"供应商"的其他费用</span
              >
            </el-form-item>

            <br />
            <el-form-item label="出车备注" prop="remark">
              <el-input
                v-model="carForm.remark"
                type="textarea"
                :rows="2"
              ></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="submitAdd">确 定</el-button>
          </span>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getDataByKey, openDB, changeDB, editDB, deleteDB } from "@/db/indexdb";
import { searchTable } from "@/db/search";
import { printData } from "@/db/print";

export default {
  data() {
    return {
      activeName: "1",
      pag1: {
        plate: "",
        is_bill1: "",
        time: [],
        plate: "",
        total: 0,
        page: 1,
        size: 10,
      },
      carsList: [],
      h: 0,
      carForm: {
        type: "1", //出车类型
        create_time: "", // 出车日期
        units: "", // 计量单位
        project_id: "", //所属项目
        project_name: "", //所属项目

        company_id: "", // 派车单位
        company: "", // 派车单位名车
        source: "", //车辆来源类型
        plate: "", //车牌号
        car_type: "", //车辆类型
        specifications: "", //规格
        cost: "", //用车成本

        unit_price: "", //单价
        agreed_time: "", // 规定工时
        true_time: "", // 实际工时

        overtime_unit: "", //加班单价
        overtime_hours: "", //加班工时
        overtime_price: "", //加班费

        other_price: "", //其他费用
        other_remark: "", //其他费用备注

        other1_price: "", //其他费用1
        other1_remark: "", //其他费用1备注

        other2_price: "", //其他费用2
        other2_remark: "", //其他费用2备注

        driver1: "", // 驾驶员
        driver1_commission: "", // 驾驶员提成比例
        driver1_price: "", // 驾驶员提成金额

        driver2: "", // 随行人员
        driver2_commission: "", // 随行人员提成比例
        driver2_price: "", // 随行人员提成金额

        total_price: "", // 总金额
        operator: "", // 操作人
        dispatcher: "", // 派车人

        is_bill: "1", // 是否签单
        content: "", // 工作内容
        remark: "", // 备注
      },
      carRules: {
        company_id: [
          { required: true, message: "请选择派车单位", trigger: "blur" },
        ],
        plate: [{ required: true, message: "请输入车牌", trigger: "blur" }],
        car_type: [
          { required: true, message: "请选择车辆类型", trigger: "blur" },
        ],
        specifications: [
          { required: true, message: "请输入车辆规格", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择出车类型", trigger: "blur" }],
        units: [{ required: true, message: "请输入计量单位", trigger: "blur" }],
        create_time: [
          { required: true, message: "请选择出车日期", trigger: "blur" },
        ],
        unit_price: [
          { required: true, message: "请输入单价", trigger: "blur" },
        ],
        cost: [{ required: true, message: "请输入用车成本", trigger: "blur" }],
        agreed_time: [
          { required: true, message: "请输入工期", trigger: "blur" },
        ],
        true_time: [
          { required: true, message: "请输入实际工时", trigger: "blur" },
        ],
      },
      concatCompany: [], // 车辆来源
      gsList: [],
      companyList: [],
      myCars: [], // 车辆登记表
      projectList: [],
      yg_name: "",
      projectInfoList: [],
      infoedit: false,
      totalPrice: 0,
      totalList: [],
      multipleSelection: [],
    };
  },
  created() {
    openDB("car", "list").then(() => {
      this.getList();
      this.getOther();
    });
    this.h = document.documentElement.offsetHeight - 320;
  },
  mounted() {
    // this.$refs.radios.$children.forEach((item) => {
    //   item.$refs.radio.removeAttribute("aria-hidden");
    // });
  },
  methods: {
    isSelectable(row, index) {
      let flag = row.is_bill1 != 1;
      return flag;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async toBill() {
      let result = [...this.multipleSelection];
      if (result.length == 0) {
        this.$message.warning("请选择签单数据");
        return;
      }
      result.map((item) => {
        item.is_bill1 = 1;
        editDB("list", "出车记录表", item).then(() => {
          this.getList();
        });
      });

      this.$message.success("录入成功");
    },

    getList() {
      getDataByKey("list", "出车记录表").then((res) => {
        let datas = { ...this.pag1 };
        datas.source = Number(datas.source || "");
        datas.is_bill1 = Number(datas.is_bill1 || "");
        let { total, list, totalList } = searchTable(
          res?.data,
          datas,
          1,
          "start_time"
        );
        console.log(list, "list");
        this.carsList = list;
        this.pag1.total = total;
        let p = 0;
        totalList.map((item) => {
          p = p + Number(item.total_price || 0);
        });
        this.totalPrice = p;
        this.totalList = totalList;
      });
    },
    async getOther() {
      getDataByKey("list", "项目表").then((res) => {
        this.projectList = res?.data;
        console.log(res.data, "data");
      });
      getDataByKey("list", "车辆登记表").then((res) => {
        this.myCars = res?.data;
      });

      await getDataByKey("list", "供应商表").then(async (res1) => {
        this.gsList = res1?.data || [];
        await this.gsList.map((v) => {
          v.source = 2;
        });
      });
      await getDataByKey("list", "公司信息表").then(async (res) => {
        this.companyList = res?.data || [];
        await this.companyList.map((v) => {
          v.source = 1;
        });
      });
      getDataByKey("list", "员工表").then((res) => {
        this.yg_name = res?.data;
      });
      getDataByKey("list", "员工表").then((res) => {
        this.yg_name = res?.data;
      });
      this.concatCompany = this.companyList.concat(this.gsList || []);
    },
    resetSearch() {
      this.pag1 = {
        plate: "",
        is_bill1: "",
        time: [],
        plate: "",
        total: 0,
        page: 1,
        size: 10,
      };
      this.getList();
    },
    addOneCar() {
      this.activeName = "2";
    },
    changeGroup(val) {
      this.carForm.type = val;
      this.carForm.create_time = "";
      this.carForm.overtime = "";
      this.carForm.overtime_unit = "";
      this.carForm.unit_price = "";
      this.carForm.total_price = "";
      this.carForm.agreed_time = "";
      this.carForm.true_time = "";
      this.carForm.units = val == 1 ? "小时" : "天";
    },
    changeGs(val) {
      let item = this.concatCompany.filter((v) => {
        return v.id == val;
      })[0];
      this.carForm.company = item.name;
      this.carForm.source = item.source;
      this.carForm.car_type = "";
      this.carForm.specifications = "";
      this.carForm.plate = "";
    },
    async changeplate(val) {
      let item = await this.myCars.filter((v) => {
        return v.id == val;
      })[0];
      this.carForm.car_id = val;
      this.carForm.plate = item.plate;
      this.carForm.car_type = item.type;
      this.carForm.specifications = item.specifications;
      let priceItem = await this.projectInfoList.filter((p) => {
        return p.jixie_type == item.type && p.guige == item.specifications;
      })[0];
      this.carForm.unit_price = priceItem ? priceItem.untis1 : "";
      this.carForm.agreed_time = priceItem ? priceItem.worktime : "";
      this.carForm.overtime_unit = priceItem ? priceItem.overtime_untis : "";
      this.changeMoney(2);
    },
    changeMoney(key) {
      let item = this.carForm;
      if (key == 2) {
        this.carForm.true_time = item.agreed_time;
      }
      // this.carForm.overtime_unit =
      //   this.carForm.type == 1
      //     ? Number(item.unit_price || 0) / Number(item.agreed_time || 0)
      //     : Number(this.carForm.overtime_unit || 0);
      this.carForm.total_price =
        (Number(item.unit_price || 0) / Number(item.agreed_time || 0)) *
          Number(item.true_time || 0) +
        Number(item.overtime_price || 0) +
        Number(item.other_price || 0);
      this.changeCommission();
    },
    changeOverTime() {
      let item = this.carForm;
      if (
        Number(item.overtime_unit || 0) > 0 &&
        Number(item.overtime_hours || 0) > 0
      ) {
        this.carForm.overtime_price =
          Number(item.overtime_unit || 0) * Number(item.overtime_hours || 0);
        this.carForm.total_price =
          (Number(item.unit_price || 0) / Number(item.agreed_time || 0) || 0) *
            Number(item.true_time || 0) +
          Number(item.overtime_unit || 0) * Number(item.overtime_hours || 0);
        this.carForm.overtime_price = this.carForm.overtime_price.toFixed(4);
        this.carForm.total_price = this.carForm.total_price.toFixed(4);
      } else {
        this.carForm.overtime_price = "";
      }
    },
    changeCommission() {
      let item = this.carForm;
      if (Number(item.driver1_commission || 0) > 0) {
        this.carForm.driver1_price =
          Number(item.unit_price || 0) * Number(item.driver1_commission || 0);
        this.carForm.driver1_price = this.carForm.driver1_price.toFixed(4);
      } else {
        this.carForm.driver1_price = "";
      }
      if (Number(item.driver2_commission || 0) > 0) {
        this.carForm.driver2_price =
          Number(item.unit_price || 0) * Number(item.driver2_commission || 0);
        this.carForm.driver2_price = this.carForm.driver2_price.toFixed(4);
      } else {
        this.carForm.driver2_price = "";
      }
    },
    changeProject(val) {
      let result1 = this.projectList.filter((item1) => {
        return item1.id == val;
      })[0];
      this.projectItem = result1;
      this.carForm.type = result1.contract_type;

      getDataByKey("list", "项目信息表").then((res) => {
        let list = res?.data;
        let result = list.filter((item) => {
          return item.project_id == val;
        });
        this.projectInfoList = result;
      });
      this.carForm.units = result1.contract_type == 1 ? "小时" : "天";
      // this.changeGroup(result1.contract_type);
    },
    handleClose() {
      this.carForm = {
        type: "1", //出车类型
        create_time: "", // 出车日期
        units: "", // 计量单位
        project_id: "", //所属项目
        project_name: "", //所属项目

        company_id: "", // 派车单位
        company: "", // 派车单位名车
        source: "", //车辆来源类型
        plate: "", //车牌号
        car_type: "", //车辆类型
        specifications: "", //规格
        cost: "", //用车成本

        unit_price: "", //单价
        agreed_time: "", // 规定工时
        true_time: "", // 实际工时

        overtime_unit: "", //加班单价
        overtime_hours: "", //加班工时
        overtime_price: "", //加班费

        other_price: "", //其他费用
        other_remark: "", //其他费用备注

        other1_price: "", //其他费用1
        other1_remark: "", //其他费用1备注

        other2_price: "", //其他费用2
        other2_remark: "", //其他费用2备注

        driver1: "", // 驾驶员
        driver1_commission: "", // 驾驶员提成比例
        driver1_price: "", // 驾驶员提成金额

        driver2: "", // 随行人员
        driver2_commission: "", // 随行人员提成比例
        driver2_price: "", // 随行人员提成金额

        total_price: "", // 总金额
        operator: "", // 操作人
        dispatcher: "", // 派车人

        is_bill: "1", // 是否签单
        content: "", // 工作内容
        remark: "", // 备注
      };
      this.infoedit = false;
    },
    async submitAdd() {
      let data = { ...this.carForm };
      this.$refs["carsForm"].validate((valid) => {
        if (valid) {
          if (Array.isArray(this.carForm.create_time)) {
            data.start_time = data.create_time[0];
            data.end_time = data.create_time[1];
          } else {
            data.start_time = data.create_time || "";
            data.end_time = data.create_time || "";
          }
          data.project_name = this.projectItem.project_name;
          data.project_id = this.projectItem.id;
          data.lessee = this.projectItem.lessee_name;
          data.lessee_id = this.projectItem.lessee;
          data.price1 = (
            Number(data.total_price || 0) -
            Number(data.overtime_price || 0) -
            Number(data.other_price || 0)
          ).toFixed(4);
          data.unit_price = Number(data.unit_price || 0).toFixed(4);
          if (this.infoedit) {
            editDB("list", "出车记录表", data).then(() => {
              this.addIncome(data);
              this.$message.success("修改成功");
              this.activeName = "1";
              this.getList();
            });
          } else {
            data.id = new Date().getTime();
            changeDB("list", "出车记录表", data).then(() => {
              this.addIncome(data);
              this.handleClose();
              this.$message.success("添加成功");
              this.activeName = "1";
              this.getList();
            });
          }
        } else {
          return false;
        }
      });
    },
    editCar(row) {
      this.carForm = { ...row };
      console.log(row, "row");
      this.infoedit = true;
      this.changeProject(row.project_id);
      this.activeName = "2";
    },
    delCar(row) {
      this.$confirm("确定删除该条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteDB("list", "出车记录表", row.id)
            .then(() => {
              this.$message({
                type: "success",
                message: "已删除",
              });
              this.getList();
            })
            .catch((err) => {
              this.$message.error(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async addIncome(data) {
      let chucheList = [];
      await getDataByKey("list", "出车记录表").then((res) => {
        chucheList = res?.data.filter((item) => {
          return item.project_id == data.project_id;
        });
      });
      // 增加应收应付明细
      getDataByKey("list", "收付款表").then(async (res1) => {
        // 应收 客户的项目钱  和  帮供应商垫付的钱 project_id = total_price 车车信息的id
        //  应付  客户帮垫付的钱  和 租车成本
        let incomeArr = res1?.data;
        // 项目应收-客户
        let totalPrice = 0;
        chucheList.map((item) => {
          totalPrice = Number(totalPrice) + Number(item.total_price);
        });
        // 项目基础应收 -车辆的收入
        let data1 = {
          item_type: 1,
          type: 1,
          cars_id: this.projectItem.id,
          project_id: this.projectItem.id,
          project_name: this.projectItem.project_name,
          receivable: this.projectItem.lessor_name,
          receivable_id: this.projectItem.lessor,
          payable: this.projectItem.lessee_name,
          payable_id: this.projectItem.lessee,
          total_price: Number(totalPrice).toFixed(4),
          abstract: "出车应收合计",
        };
        let flag = incomeArr.filter((item) => {
          return (
            item.cars_id == data1.cars_id &&
            item.item_type == 1 &&
            item.type == 1
          );
        });
        if (flag.length > 0) {
          data1.id = flag[0].id;
          await editDB("list", "收付款表", data1).then(() => {});
        } else {
          data1.id = Number(new Date().getTime()) + 1;
          await changeDB("list", "收付款表", data1).then(() => {});
        }
        // 项目其他应收 - 帮供应商垫付的资金
        let data2;
        if (data.other1_price != "") {
          data2 = {
            item_type: 1,
            type: 2,
            cars_id: data.id,
            project_id: this.projectItem.id,
            project_name: data.project_name,
            receivable: this.projectItem.lessor_name,
            receivable_id: this.projectItem.lessor,
            payable: data.company,
            payable_id: data.company_id,
            total_price: Number(data.other1_price).toFixed(4),
            abstract: data.plate + "-" + data.other1_remark,
          };
          let flag2 = incomeArr.filter((item) => {
            return (
              item.cars_id == data2.cars_id &&
              item.item_type == 1 &&
              item.type == 2
            );
          });
          if (flag2.length > 0) {
            data2.id = flag2[0].id;
            await editDB("list", "收付款表", data2).then(() => {});
          } else {
            data2.id = Number(new Date().getTime()) + 2;
            await changeDB("list", "收付款表", data2).then(() => {});
          }
        }
        // 项目基础应付 - 租车成本
        if (data.source == 2) {
          let data3 = {
            item_type: 2,
            type: 3,
            cars_id: data.id,
            project_id: this.projectItem.id,
            project_name: this.projectItem.project_name,
            receivable: data.company,
            receivable_id: data.company_id,
            payable: this.projectItem.lessor_name,
            payable_id: this.projectItem.lessor,
            total_price: Number(data.cost).toFixed(4),
            abstract: data.plate + "-" + "租车成本",
          };
          let flag3 = incomeArr.filter((item) => {
            return (
              item.cars_id == data3.cars_id &&
              item.item_type == 2 &&
              item.type == 3
            );
          });
          if (flag3.length > 0) {
            data3.id = flag3[0].id;
            console.log(data3, "data3111");
            await editDB("list", "收付款表", data3).then(() => {
              console.log("123123");
            });
          } else {
            data3.id = Number(new Date().getTime()) + 3;
            console.log(data3, "data3222");
            await changeDB("list", "收付款表", data3).then(() => {
              console.log("44444");
            });
          }
        }
        if (data.other2_price != "") {
          let data4 = {
            item_type: 2,
            type: 4,
            cars_id: data.id,
            project_id: this.projectItem.id,
            project_name: this.projectItem.project_name,
            receivable: this.projectItem.lessee_name,
            receivable_id: this.projectItem.lessee,
            payable: this.projectItem.lessor_name,
            payable_id: this.projectItem.lessor,
            total_price: Number(data.other2_price).toFixed(4),
            abstract: data.plate + "-" + data.other2_remark,
          };
          let flag4 = incomeArr.filter((item) => {
            return (
              item.cars_id == data4.cars_id &&
              item.item_type == 2 &&
              item.type == 4
            );
          });
          if (flag4.length > 0) {
            data4.id = flag4[0].id;
            await editDB("list", "收付款表", data4).then(() => {});
          } else {
            data4.id = Number(new Date().getTime()) + 4;
            await changeDB("list", "收付款表", data4).then(() => {});
          }
        }
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.type == 2) {
        return "warning-row";
      } else if (row.type == 1) {
        return "";
      }
      return "";
    },

    // handleSelectionChange(val) {
    //   this.printList = val;
    // },
    printListData() {
      let header = [
        { value: "create_time", label: "日期", first: 1 },
        { value: "company", label: "派车单位" },
        { value: "plate", label: "车牌" },
        { value: "car_tpe", label: "车型" },
        { value: "specifications", label: "规格" },
        { value: "cost", label: "用车成本", total: 1, type: "n" },
        { value: "units", label: "计量单位" },
        { value: "unit_price", label: "单价", type: "n" },
        { value: "agreed_time", label: "额定工时", type: "n" },
        { value: "true_time", label: "实际工时", type: "n" },
        { value: "price1", label: "金额", total: 1, type: "n" },
        { value: "overtime_unit", label: "加班单价", type: "n" },
        { value: "overtime_time", label: "加班时长", type: "n" },
        { value: "overtime_price", label: "加班费", total: 1, type: "n" },
        {
          value: "other_price",
          label: "其他费用(收客户)",
          total: 1,
          type: "n",
        },
        { value: "other_remark", label: "其他费用说明(收客户)" },
        { value: "total_price", label: "合计金额", total: 1, type: "n" },
        {
          value: "other1_price",
          label: "其他费用(应收供应商)",
          total: 1,
          type: "n",
        },
        { value: "other1_remark", label: "费用说明(应收供应商)" },
        {
          value: "other2_price",
          label: "其他费用(应付客户)",
          total: 1,
          type: "n",
        },
        { value: "other2_remark", label: "费用说明(应付客户)" },
        { value: "content", label: "工作内容" },
        { value: "remark", label: "说明" },
      ];
      let msg = {
        msg1: "出车记录",
      };
      printData(msg, this.totalList, header);
    },
    handleSizeChange1(val) {
      this.pag1.size = val;
      this.getList();
    },
    handleCurrentChange1(val) {
      this.pag1.page = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.search_box {
  display: flex;
  flex-wrap: wrap;
  border-bottom: 1px solid #ccc;
  .el-input,
  .el-select {
    width: 200px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-date-editor {
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-button {
    margin-bottom: 20px;
  }
}
.export_box {
  display: flex;
  justify-content: flex-end;
  margin: 10px 0;
}
.page_table {
  margin-top: 20px;
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
.el-form {
  .el-select,
  .el-input,
  .el-date-editor {
    width: 160px;
  }
  .el-textarea {
    width: 660px;
  }
  .el-range-editor {
    width: 290px;
  }
}
.pro_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}
</style>
<style>
.el-table .warning-row {
  background: #fdf6ec;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
